package com.blockchain.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;

@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception{
        http
                .cors(cors->cors
                        .configurationSource(request -> {
                            CorsConfiguration config=new CorsConfiguration();
                            config.setAllowedOrigins(Arrays.asList("http://localhost:8080","http://47.113.225.21")); // 允许的源
                            config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE")); // 允许的方法
                            config.setAllowedHeaders(Arrays.asList("Authorization", "Content-Type")); // 允许的头部信息
                            return config;
                        }))
                .csrf(csrf->csrf.disable());
        return http.build();
    }
}